<?php

namespace AdminBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Doctrine\ORM\EntityRepository;
use AdminBundle\Utility\Constant;

class BaseWebchatUserType extends AbstractType
{
    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('nickname', TextType::class, [
            'label' => '昵称',
            'label_attr' => Constant::LABEL_ATTR_DEFAULT,
            'attr' => [
                'placeholder' => '请输入昵称',
                'class' => Constant::INPUT_ATTR_CLASS],
            'required' => false,
        ])->add('note', TextareaType::class, [
            'label' => '注释',
            'label_attr' => Constant::LABEL_ATTR_DEFAULT,
            'attr' => [
                'placeholder' => '请输入注释',
                'class' => Constant::INPUT_ATTR_CLASS],
            'required' => false,
            'empty_data' => '',
        ])->add('isEnabled', CheckboxType::class, array(
                'label' => '启用',
                'label_attr' => Constant::LABEL_ATTR_DEFAULT,
                'attr' => [
                    'class' => 'ace ace-switch ace-switch-5',],
                'required' => false,
            )
        )->add('userName', TextType::class, [
            'label' => '微信号',
            'label_attr' => Constant::LABEL_ATTR_DEFAULT,
            'attr' => [
                'placeholder' => '请输入微信号',
                'class' => Constant::INPUT_ATTR_CLASS],
            'required' => true,
        ])->add('mobile', TextType::class, [
            'label' => '手机号',
            'label_attr' => Constant::LABEL_ATTR_DEFAULT,
            'attr' => [
                'placeholder' => '请输入手机号',
                'class' => Constant::INPUT_ATTR_CLASS],
            'required' => false,
            'empty_data' => '',
        ])->add('password', TextType::class, [
            'label' => '密码',
            'label_attr' => Constant::LABEL_ATTR_DEFAULT,
            'attr' => [
                'placeholder' => '请输入密码',
                'class' => Constant::INPUT_ATTR_CLASS],
            'required' => false,
            'empty_data' => '',
        ])->add('country', TextType::class, [
            'label' => '国家',
            'label_attr' => Constant::LABEL_ATTR_DEFAULT,
            'attr' => [
                'placeholder' => '请输入国家',
                'class' => Constant::INPUT_ATTR_CLASS],
            'required' => false,
            'empty_data' => '',
        ])->add('province', TextType::class, [
            'label' => '省份',
            'label_attr' => Constant::LABEL_ATTR_DEFAULT,
            'attr' => [
                'placeholder' => '请输入省份',
                'class' => Constant::INPUT_ATTR_CLASS],
            'required' => false,
            'empty_data' => '',
        ])->add('city', TextType::class, [
            'label' => '城市',
            'label_attr' => Constant::LABEL_ATTR_DEFAULT,
            'attr' => [
                'placeholder' => '请输入城市',
                'class' => Constant::INPUT_ATTR_CLASS],
            'required' => false,
            'empty_data' => '',
        ])->add('signature', TextareaType::class, [
            'label' => '个性签名',
            'label_attr' => Constant::LABEL_ATTR_DEFAULT,
            'attr' => [
                'placeholder' => '请输入个性签名',
                'class' => Constant::INPUT_ATTR_CLASS],
            'required' => false,
            'empty_data' => '',
        ])->add('sex', ChoiceType::class, [
            'label' => '性别',
            'label_attr' => Constant::LABEL_ATTR_DEFAULT,
            'attr' => [
                'placeholder' => '请输入性别',
                'class' => Constant::INPUT_ATTR_CLASS],
            'required' => false,
            'empty_data' => '',
            'choices' => Constant::SEX,
        ])->add('headImgUrl', TextType::class, [
            'label' => '头像',
            'label_attr' => Constant::LABEL_ATTR_DEFAULT,
            'attr' => [
                'placeholder' => '请输入头像',
                'class' => Constant::INPUT_ATTR_CLASS],
            'required' => false,
            'empty_data' => '',
        ])->add('email', TextType::class, [
            'label' => '邮件',
            'label_attr' => Constant::LABEL_ATTR_DEFAULT,
            'attr' => [
                'placeholder' => '请输入邮件',
                'class' => Constant::INPUT_ATTR_CLASS],
            'required' => false,
            'empty_data' => '',
        ])->add('deviceImei', EntityType::class, [
            'label' => '终端',
            'label_attr' => Constant::LABEL_ATTR_DEFAULT,
            'attr' => [
                'placeholder' => '请选择终端',
                'class' => "chosen-select form-control"],
            'required' => false,
            'empty_data' => "",
            // input=choice|select , must be require
            'em' => 'default',
            'class' => 'AdminBundle:BaseDevice',
            'query_builder' => function (EntityRepository $repo) {
                return $repo->createQueryBuilder('u')->where('u.isEnabled=1')->orderBy('u.id', 'desc');
            },
            'choice_label' => function ($baseDataSource) {
                return $baseDataSource->getName() . '--' . $baseDataSource->getNote();
            },
            'choice_value' => 'imei',
            'choice_name' => 'id',
            'expanded' => false,
            'multiple' => false,
        ]);
//        $builder->add('nickname')->add('userName')->add('mobile')->add('password')->add('note')->add('isEnabled')->add('creator')->add('ip')->add('ctime')->add('country')->add('province')->add('city')->add('signature')->add('sex')->add('headImgUrl')->add('email')->add('deviceId')->add('deviceText');
    }
    
    /**
     * {@inheritdoc}
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'AdminBundle\Entity\BaseWebchatUser'
        ));
    }

    /**
     * {@inheritdoc}
     */
    public function getBlockPrefix()
    {
        return 'adminbundle_basewebchatuser';
    }


}
